package com.jsz.peini.common.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DateTimeUtils {
	private static final Logger logger = LoggerFactory.getLogger(DateTimeUtils.class);
	public static String formatDateTime2String(Date date,String formatStr){
		SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
		if(date == null){
			return sdf.format(new Date());
		}else{
			return sdf.format(date);
		}
	}
	
	public static String formatDateTime2LongString(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(date == null){
			return sdf.format(new Date());
		}else{
			return sdf.format(date);
		}
	}
	
	public static String formatDateTime2ShortString(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if(date == null){
			return sdf.format(new Date());
		}else{
			return sdf.format(date);
		}
	}
	
	public static Date formatString2Date(String str,String formatStr){
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
			if(StringUtils.isNotBlank(str)){
				return sdf.parse(str);
			}else{
				return null;
			}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return null;
	}
	
	/**
	 * 获取周
	 * @return
	 */
	public static int getWeek(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        if(w == 0){
        	w = 7;
        }
        return w;
	}
	
	public static List<Date> findDates(Date dBegin, Date dEnd){  
		List lDate = new ArrayList();  
		lDate.add(dBegin);
		Calendar calBegin = Calendar.getInstance();  
		// 使用给定的 Date 设置此 Calendar 的时间  
		calBegin.setTime(dBegin);  
		Calendar calEnd = Calendar.getInstance();  
		// 使用给定的 Date 设置此 Calendar 的时间  
		calEnd.setTime(dEnd);  
		// 测试此日期是否在指定日期之后  
		while (dEnd.after(calBegin.getTime())){  
			// 根据日历的规则，为给定的日历字段添加或减去指定的时间量  
			calBegin.add(Calendar.DAY_OF_MONTH, 1);  
			lDate.add(calBegin.getTime());  
		}  
		return lDate;  
	}
}
